package com.cn.yunhe.rentcars.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.cn.yunhe.rentcars.mapper.WelcomeMapper;
import com.cn.yunhe.rentcars.model.CarCountByState;
import com.cn.yunhe.rentcars.model.City;
/**
 * 欢迎页面 信息查询，controller
 * @author 赵志学
 *2016-8-4
 */
@RestController
public class WelcomeController {

	@Autowired
	private WelcomeMapper welcomeMapper;
	
	@RequestMapping(value="/welcome", method=RequestMethod.GET)
	public ModelAndView welcome(HttpServletRequest request) {
		
		//TODO：处理业务数据
		
		
		//TODO:从会话中获取用户信息
		
		
		//TODO:查询业务数据逻辑，获取结果
		
		
		//TODO:设置到模型属性，返回页面
		Subject subject = SecurityUtils.getSubject();
		String lastLoginIp = subject.getSession().getHost();
		String name = (String) subject.getSession().getAttribute("username");
		Map<String,Object> map = (Map<String, Object>) subject.getSession().getAttribute("modelT");
		//System.out.println("shij :"+map.get("time"));
		//System.out.println("名字:"+name);
		//Map<String,Object> model = (Map<String, Object>) subject.getSession().getAttribute("model");
		Date st = subject.getSession().getLastAccessTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
		String lastLoginTime = sdf.format(st);
		String hostName = (String) subject.getSession().getHost();
		String hostUrl = request.getServerName();
		String htmlName = request.getRequestURI();
		String path = request.getContextPath();
		Integer serverPort = request.getServerPort();
		String contextPath = request.getScheme()+ "://" + request.getServerName() + ":" + request.getServerPort() + path + "";
		
		//车辆信息统计
		//System.out.println("信息统计");
	
		Map<String,Object> modelMap = new HashMap<String,Object>();
		modelMap.put("loginNum", "28");
		modelMap.put("lastLoginIp", lastLoginIp);
		modelMap.put("lastLoginTime", lastLoginTime);
		modelMap.put("hostName", hostName);//主机名
		modelMap.put("hostUrl", hostUrl);//主机地址
		modelMap.put("htmlName", contextPath);//
		modelMap.put("serverPort", serverPort);//端口号
		
		//添加昨日车辆信息，传递到页面
		modelMap.put("carMassage", getCarStateCount());
		//今日车辆信息 首页统计。	
		modelMap.put("todayCar", getToday());	

		return new ModelAndView("welcome",modelMap);
	}
	/**
	 * 昨日车辆信息状态查询。
	 * @return
	 */
	public CarCountByState getCarStateCount(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		long da = date.getTime()-86400L;			
		String yesterday = sdf.format(da);
		CarCountByState ccs = new CarCountByState();
		
		int carstate = 0;
		//车辆总数l
		ccs.setCountCar(welcomeMapper.getCarCountByState(carstate)); 
		//车辆昨日维修数量。
		ccs.setMainTainCar(welcomeMapper.getCarRepair(yesterday));
		//昨日保养车辆
		ccs.setUpKeepCar(welcomeMapper.getCarMaintain(yesterday));
		//昨日年检车辆
		ccs.setYearcheck(welcomeMapper.getCarYearCheck(yesterday));
		//昨日自用车辆
		ccs.setSelfCar(welcomeMapper.getCarSelfuse(yesterday));
		//昨日在租车辆
		ccs.setCarOrder(welcomeMapper.getCarOrder(yesterday));
		return ccs;
	}	
	public CarCountByState getToday(){
		CarCountByState ccs = new CarCountByState();
		//车辆总数l
		ccs.setCountCar(carCount()); 		
		//今辆昨日维修数量。
		ccs.setMainTainCar(maintainCar());		
		//今日保养车辆
		ccs.setUpKeepCar(nowUpkeepCar());		
		//今日年检车辆
		ccs.setYearcheck(yearCheck());		
		//今日自用车辆
		ccs.setSelfCar(selfCarCount());		
		//今日在租车辆
		ccs.setCarOrder(carOrderCount());	
		return ccs;
	}
	/**
	 * 计算公司旗下所有车辆总数。
	 * @return
	 */
	public int carCount(){
		int carstate = 0;
		int count = welcomeMapper.getCarCountByState(carstate);
		return count;
	}
	/**
	 * 统计在租的车辆 有多少辆
	 * @return
	 */
	public int carOrderCount(){
		int carState = 11;
		int count = welcomeMapper.getCarCountByState(carState);
		return count;
	}
	/**
	 * 统计自用车辆有多少辆。
	 * @return
	 */
	public int selfCarCount(){
		int carstate = 7;
		int count = welcomeMapper.getCarCountByState(carstate);
		return count;
	}
	/**
	 * 统计在保养的车辆有多少
	 * @return
	 */
	public int nowUpkeepCar(){
		int carstate = 9;
		int count = welcomeMapper.getCarCountByState(carstate);
		return count;
	}
	/**
	 * 查询正在维修的车辆数量
	 * @return
	 */
	public int maintainCar(){
		int carstate = 8;
		int count = welcomeMapper.getCarCountByState(carstate);
		return count;
	}
	/**
	 * 年检中的车辆数量
	 * @return
	 */
	public int yearCheck(){
		int carstate = 10;
		int count = welcomeMapper.getCarCountByState(carstate);
		return count;
	}

	@RequestMapping(value="/welcome2", method=RequestMethod.GET)
	public City getWelcomeData() {
		
		//TODO：处理业务数据
		
		
		//TODO:从会话中获取用户信息
		
		
		//TODO:查询业务数据逻辑，获取结果
		
		
		//TODO:设置到模型属性，返回页面

		
		return new City("1","zsl","正常");
	}
	
}
